# coding: utf-8
from services.user import UserService
from services.db import DBService
from django.conf import settings
import threading


class OnlineMiddleware(object):
    def process_request(self, request):
        if request.session.has_key('_auth_user_id'):    # 已经登录
            redis_db = DBService.get_redis_db()

            current_ids = redis_db.lrange(settings.REDIS_USERS_KEY, 0, -1)
            if not request.session.get('_auth_user_id') in current_ids:
                redis_db.lpushx(settings.REDIS_USERS_KEY, request.session.get('_auth_user_id'))

            activity_users_count = redis_db.llen(settings.REDIS_USERS_KEY)

            # 参数传递------------------------
            current_thread = threading.currentThread()
            current_thread.z_user = request.user   #

            # print '=====', redis_db.llen(settings.REDIS_USERS_KEY)

    # def process_response(self, request, response):
    #     print '--------', response
    #     return response